<?php
// +----------------------------------------------------------------------
// | Jqcms [ Avenue to Jane ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://jqcms.net All rights reserved.
// +----------------------------------------------------------------------
// | Author: hyacinth <845077398@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Model;
use Think\Model;

class ConfigModel extends Model{
    
    //验证
   /*  protected $_validate =array(
      
        //array('web_title','require','请设置网站标题!'),
        array('web_theme','require','请设置模板目录!'),
        array('web_picsize','piczy','请设置缩略图尺寸!',0,'callback'),
   
    ); */
    //写入
    public function webconfig(){
    
        $data = I('data');
        $data['web_keyword'] = $this->web_keyword();
        $data['web_logo'] = $this->upload();
        $data['web_picsize'] = $this->picsize();
        //遍历写入数据库
        foreach ($data as $k => $v) {
            $this->where(array('name'=>$k))->setField('value',$v);
            //$data[$k]=htmlspecialchars_decode($v);//实体转字符
        }
        foreach ($data as $kk=>$vv){
            $conf[$kk] = $vv;
        }
        //文件内容
        $str=<<<php
<?php
return array(
//此配置项为自动生成请勿直接修改；如需改动请在后台网站设置
//*************************************网站设置****************************************
    'WEB_TITLE'         =>  '{$conf['web_title']}',        //网站标题
    'WEB_KEYWORD'       =>  '{$conf['web_keyword']}',        //网站关键字
    'WEB_DESC'          =>  '{$conf['web_desc']}',        // 网站描述
    'WEB_LOGO'          =>  '{$conf['web_logo']}',       //LOGO
    'WEB_PICSIZE'       =>  '{$conf['web_picsize']}',     //缩略图尺寸
    'WEB_THEME'         =>  '{$conf['web_theme']}',       //主题目录
    'WEB_DOMAIN'        =>  '{$conf['web_domain']}',       //网站域名
    'WEB_TIT'           =>  '{$conf['web_tit']}',       //网站标题
    'WEB_COMPANY'       =>  '{$conf['web_company']}',       //公司名称
    'WEB_PHONE'         =>  '{$conf['web_phone']}',        //电话
    'WEB_COPYRIGHT'     =>  '{$conf['web_copyright']}',       //版权
    'WEB_MOBILE'     =>  '{$conf['web_mobile']}',       //移动版域名
     
);
php;
        file_put_contents('./Jqcms/Common/Conf/webconfig.php', $str);
        return true;
    }
    //seo_keywords
    public function web_keyword(){
        $web_keyword = I('web_keyword');
        if($web_keyword!=null){
            $keywords = preg_replace("/(\n)|(\s)|(\t)|(\')|(')|(，)/",',',$web_keyword);
            $keywords = explode(',', $keywords);
            $keywords = array_filter($keywords);
            $keywords = array_unique($keywords);
            $keywords = implode(',', $keywords);
            return $keywords;
        }else {
            return '';
        }
    
    }
    //缩略图验证
    public function piczy(){
      
        if($_POST['web_picsize'][0]==null || $_POST['web_picsize'][1]==null){
            return false;
        }else{
            return true;
        }
        
    }
    //获取模板目录
    public function theme(){
        
        $dir = "./Theme/";
        $list = scandir($dir);
        foreach ($list as $v){
            if(is_dir($dir.$v)){
                $data[]=$v;
            }else{
                continue;
            }
        }
        unset($data[0],$data[1]);
        return $data;
        
    }
    //转存
    public function picsize(){
        
        return implode(',', $_POST['web_picsize']);
        
    }
    //logo
    public function upload(){
    
        $web_logo = $_FILES['web_logo'];
        
        if($web_logo['name']){
             
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize   =     3145728 ;// 设置附件上传大小
            $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
            $upload->savePath  =     'logo/'; // 设置附件上传（子）目录
            $upload->autoSub = false;//禁止子目录自动保存
            //$upload->subName = array('date','Ymd');//子目录创建方式
            $upload->saveName  = date('Ymd',time()).'_'.mt_rand();
            // 上传文件
            $info   =   $upload->uploadOne($web_logo);
            if(!$info) {// 上传错误提示错误信息
                //$this->error($upload->getError());
            }else{// 上传成功
                 
                //$this->success('上传成功！');
            }
            return $info['savepath'].$info['savename'];
             
        }else {
             
            return $_POST['_logo'];
        }
    
    }
    
    
}